package com.tim.test.leetcode.simulation.huawei;

import java.util.Arrays;
import java.util.Random;

/**
 * @author
 * @Title:
 * @Package
 * @date 2023/6/10 17:47
 * @Description:
 */
public class Simulation5 {
    public static void main(String[] args) {
        Simulation5 s = new Simulation5();
//        s.game();
        s.getMaxWeight();
    }

    // 跳飞机游戏
    // 三数问题。排序，固定首位，对撞指针，boolean[]标记已用
    // 注意终止循环的条件，continue变参
    public void game(){
        int count = 10;
        int[] steps = new Random()
                .ints(50, 1, 8)
                .toArray();
        System.out.println(Arrays.toString(steps));
        Arrays.sort(steps);
        boolean[] flags = new boolean[steps.length];
        Arrays.fill(flags, true);
        int times = 0;
        for (int i = 0; i < steps.length-2; i++) {
            int gap = count - steps[i];
            int end = steps.length-1;
            for (int j = i+1; j < end;) {
                if(!flags[j]){
                    j++;
                    continue;
                }
                if(!flags[end]){
                    end--;
                    continue;
                }
                int temp = steps[j]+steps[end];
                if(temp==gap){
                    // 记录次数；标记使用
                    times++;
                    flags[i]=false;
                    flags[j]=false;
                    flags[end]=false;
                    if(times==count){
                        System.out.println("SUCCESS");
                        return;
                    }
                    break;
                }else if(temp>gap){
                    end--;
                }else{
                    j++;
                }
            }
        }
        System.out.println("FAIL");
    }

    // 获取最大重量的积木，sum = w1 ^ w2;
    // bSolo==bKoko；bTotal = bSolo ^ bKoko = 0；
    // bTotal = w1 ^...wn = w1 ^ (w2^...wn) = 0；取最小值即可
    public void getMaxWeight(){
        int[] weights = new Random()
                .ints(20, 1, 50)
                .toArray();
        int bTotal = weights[0];
        for (int i = 1; i < weights.length; i++) {
            bTotal = bTotal ^ weights[i];
        }
        if(bTotal!=0){
            System.out.println("NO");
            return;
        }
        Arrays.sort(weights);
        int max = Arrays.stream(weights).skip(1)
                .sum();
        System.out.println(max);
    }

}
